package com.see.network.impl;

import com.google.inject.Inject;
import com.see.domain.model.Symbol;
import com.see.network.api.StaticDataUpdator;

public class StaticDataUpdatorImpl implements StaticDataUpdator {

    // http://finance.yahoo.com/q/pr?s=BHP.AX+Profile
    private final static String BASE_URL               = "http://finance.yahoo.com/q/pr?s=";
    private final static String PROFILE_POSTFIX        = "+Profile";

    private final static String SECTOR_ANCHOR          = ">Sector:<";
    private final static String INDUSTRY_ANCHOR        = ">Industry:<";
    private final static String COMPANY_NAME_ANCHOR    = ">Search for share prices<";

    private final static String FIELD_START_TAG        = ".html\">";
    private final static String FIELD_END_TAG          = "</a>";

    private final static String COMPANY_NAME_START_TAG = "<b>";
    private final static String COMPANY_NAME_END_TAG   = "</b>";

    //private final static String NO_PROFILE_FOUND_PATTERN = "There is no Profile data available for ";
    private final static String INVALID_VALUE          = "N/A";

    private final HtmlReader    htmlReader;

    @Inject
    public StaticDataUpdatorImpl(HtmlReader htmlReader) {
        this.htmlReader = htmlReader;
    }

    public Symbol fetchSectorAndIndustryBySymbol(String inputSymbol) {

        String targetUrl = getTargetProfileURL(inputSymbol);
        String content = htmlReader.readHtml(targetUrl, false);

        String sectorString = extractTargetValue(SECTOR_ANCHOR, content);
        String industryString = extractTargetValue(INDUSTRY_ANCHOR, content);
        String companyNameString =
            extractTargetValue(COMPANY_NAME_ANCHOR, content, COMPANY_NAME_START_TAG, COMPANY_NAME_END_TAG);

        Symbol symbol = getSymbol(inputSymbol, sectorString, industryString, companyNameString);
        return symbol;
    }

    private Symbol getSymbol(String inputSymbol, String sectorString, String industryString, String companyNameString) {
        Symbol symbol = new Symbol();
        symbol.setSector(sectorString);
        symbol.setIndustry(industryString);
        symbol.setCompanyName(companyNameString);
        symbol.setTicker(inputSymbol);
        return symbol;
    }

    private String extractTargetValue(String anchor, String content, String startTag, String endTag) {
        int targetOffset = content.indexOf(anchor) + anchor.length();
        String trimContent = content.substring(targetOffset);

        int startOffset = trimContent.indexOf(FIELD_START_TAG) + FIELD_START_TAG.length();
        int endOffset = trimContent.indexOf(FIELD_END_TAG);
        String targetValue = "";
        try {
            targetValue = trimContent.substring(startOffset, endOffset).trim();
        } catch (Exception e) {
            targetValue = INVALID_VALUE;
        }

        return targetValue;
    }

    private String extractTargetValue(String anchor, String content) {
        return extractTargetValue(anchor, content, FIELD_START_TAG, FIELD_END_TAG);
    }

    private String getTargetProfileURL(String symbol) {
        return BASE_URL + symbol + PROFILE_POSTFIX;
    }
}
